var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        dept:{
            parentName:null,
            pid:0
        }
    },
    methods: {
        getDept: function(styly){
            //加载部门树
            $.get(baseURL + "sys/btcustomernavbar/select", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r.deptList);
                var node = ztree.getNodeByParam("id", vm.dept.id);
                ztree.selectNode(node);
                if(styly==1){
                    vm.dept.id = null;
                    vm.dept.pid = node.id;
                    vm.dept.name = "";
                    vm.dept.isHassub="0";
                }
                vm.dept.parentName = node.name;
            })
        },
        add: function(){
            var deptId = getDeptId();
            if(deptId == null){
                return ;
            }
            $.get(baseURL + "sys/btcustomernavbar/info/"+deptId, function(r){
                if(r.dept.isHassub=="0"){
                    alert("不能为该项添加子类");
                    return;
                }
                vm.showList = false;
                vm.title = "添加";
                vm.dept = r.dept;
                vm.getDept(1);
            });
        },
        update: function () {
            var deptId = getDeptId();
            if(deptId == null){
                return ;
            }

            $.get(baseURL + "sys/btcustomernavbar/info/"+deptId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.dept = r.dept;

                vm.getDept(2);
            });
        },
        del: function () {
            var deptId = getDeptId();
            if(deptId == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/btcustomernavbar/delete",
                    data: "deptId=" + deptId,
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.dept.id == null ? "sys/btcustomernavbar/save" : "sys/btcustomernavbar/update";
            vm.dept.isDisp = selectsord;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.dept),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            if(vm.dept.id != null)
                                vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择分类",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.dept.pid = node[0].id;
                    vm.dept.parentName = node[0].name;
                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            Dept.table.refresh();
        },
        changeType :function (ele) {
            selectsord = ele.target.value;
        }
    }
});
var selectsord='1';
var Dept ={
    id: "catemagTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Dept.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '40px'},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '60px'},
        {title: '上级名称', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '是否显示', field: 'isDisp', align: 'center', valign: 'middle', sortable: true, width: '30px',formatter:function(value, row, index){
            if(value.isDisp == "0")
                return '<span style="color:red" >'+"隐藏"+ '</span>';
            else
                return '<span style="color:green" >'+"显示"+ '</span>';
        }},
        {title: '是否有分类', field: 'isHassub', align: 'center', valign: 'middle', sortable: true, width: '30px',formatter:function(value, row, index){
            if(value.isHassub == "0")
                return '<span style="color:red" >'+"无"+ '</span>';
            else
                return '<span style="color:green" >'+"有"+ '</span>';
        }}
    ]
    return columns;
};


function getDeptId () {
    var selected = $('#catemagTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}


$(function () {
    $.get(baseURL + "sys/btcustomernavbar/info", function(r){
        var colunms = Dept.initColumn();
        var table = new TreeTable(Dept.id, baseURL + "sys/btcustomernavbar/list", colunms);
        table.setRootCodeValue(r.deptId);
        table.setExpandColumn(2);
        table.setIdField("id");
        table.setCodeField("id");
        table.setParentCodeField("pid");
        table.setExpandAll(false);
        table.init();
        Dept.table = table;
    });
});